package com.example.com.common.app;

import android.content.Context;
import android.view.View;

import com.example.com.common.utils.ToastUtil;

/**
 * mvp中基础v层的fragment
 * Created by ChenYiYao on 2018/2/1.
 */

public abstract class PresenterFragment<Presenter extends BaseContract.Presenter>
        extends BaseFragment implements BaseContract.View<Presenter> {
    protected Presenter mPresenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 在界面onAttach之后就触发初始化Presenter
        initPresenter();
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            //销毁持有p层的对象
            mPresenter.destroy();
        }
        super.onDestroy();
    }

    /**
     * 初始化Presenter
     *
     * @return Presenter
     */
    protected abstract Presenter initPresenter();


    @Override
    public void showError(String str) {
        ToastUtil.showToastLong(getContext(), str);
    }

    @Override
    public void showLoading(String str) {
        showProgressDialog(str);
    }

    @Override
    public void showLoading(String str, boolean flag) {
        showProgressDialog(str, false);
    }

    @Override
    public void dimissLoading() {
        dismissProgressDialog();
    }

    @Override
    public void setPresenter(Presenter presenter) {
        // View中赋值Presenter
        mPresenter = presenter;
    }

    @Override
    public void showSnackBarWarning(String str) {
    }
}
